package com.sgb.kjwl.viewmodel.self.company

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.sgb.kjwl.api.APIManager
import com.swgk.core.base.api.ICallBack
import com.sgb.kjwl.api.service.UserAPI
import com.swgk.core.util.PickSelectEntity
import com.sgb.kjwl.model.entity.self.ManagementDynamicEntity
import com.swgk.core.base.model.entity.ListResponse
import com.swgk.core.base.BaseViewModel
import com.swgk.core.base.model.entity.BaseEntity
import com.swgk.core.dialog.DialogHelper
import retrofit2.Call


class ManagementDynamicViewModel : BaseViewModel() {

    private var mTypes  = ArrayList<PickSelectEntity<Any>>()

    val mTypeDatas = MutableLiveData<List<PickSelectEntity<Any>>>()

    val mManagementDynamicList = MutableLiveData<List<ManagementDynamicEntity>>()

    private var page = 1//起始页
    private var limit = 10//每页加载个数

    private val map : HashMap<String, Any> = HashMap()

    //获取全部类型
    fun getManagementDynamicTypes(names : ArrayList<String>){
        var entity : PickSelectEntity<Any>?= null
        mTypes.clear()
        for (i in names.indices){
            entity = PickSelectEntity()
            entity.projectscale = names[i]
            if ("全部" == names[i]){
                entity.no = ""
            } else if ("资产" == names[i]) {
                entity.no = "8"
            } else {
                entity.no = "" + i
            }
            mTypes.add(entity)
        }
        mTypeDatas.postValue(mTypes)
    }

    //根据类型查看数据
    fun getManagementDynamicList(context: Context, type : String, isLoadMore : Boolean){
        page = if (isLoadMore) page + 1 else 1
        map["page"] = page
        map["limit"] = limit
        map["type"] = type
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null)
            .setCanceledOnTouchOutside(false)
        APIManager.getInstance().userAPI.getManagementDynamicList(UserAPI.UserCenterUrl.URL_MANAGEMENT_DYNAMIC, map).
        enqueue(object : ICallBack<BaseEntity<ListResponse<List<ManagementDynamicEntity>>>>(){
            override fun onSuccess(data: BaseEntity<ListResponse<List<ManagementDynamicEntity>>>?) {
                DialogHelper.dismissProgressDialog()
                if (data == null || data.data == null || data.data.list == null){
                    mManagementDynamicList.postValue(ArrayList())
                } else {
                    mManagementDynamicList.postValue(data.data.list)
                }
            }

            override fun onFail() {
                mManagementDynamicList.postValue(ArrayList())
                //super.onFail()
            }

            override fun onFailure(call: Call<BaseEntity<ListResponse<List<ManagementDynamicEntity>>>>, t: Throwable) {
                //super.onFailure(call, t)
            }
        })
    }
}